package tool

import (
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/request"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	"MoSkeleton/web/pogos/request/tool"
	"MoSkeleton/web/v1/http/base"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type GenBizCtrl struct {
	base.BaseCtrl
}

func (g *GenBizCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleGenBizMsg)
	return msg
}

// AddGenBiz 新增业务信息
// @Tags 业务信息
// @Summary 添加业务信息
// @Security ApiKeyAuth
// @Param data body tool.GenBizAddReq true "新增业务信息"
// @Produce  application/json
// @Success 200 {object} response.Response{data=tool.GenBizItemResp,msg=string} "{"code": 200, "data": [...]}"
// @Router /tool/codegen/biz [post]
func (g *GenBizCtrl) AddGenBiz(c *gin.Context) {
	addReq := new(tool.GenBizAddReq)
	if err := c.ShouldBindJSON(addReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	addReq.CreatedBy = utils.GetUsernameFromGin(c)
	addReq.UpdatedBy = addReq.CreatedBy
	if ok, err := services.ServiceGroupInst.ToolGroupInst.GenBizServiceInst.AddGenBiz(addReq); err != nil {
		global.MoLogger.Error("添加业务信息失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_BizAddFailMsg)
		return
	} else {
		if ok {
			response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
			return
		} else {
			response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_CommCreatFailMsg)
		}
	}
}

// UpdateGenBizById 更新业务信息
// @Tags 业务信息
// @Summary 更新业务信息
// @Security ApiKeyAuth
// @Param data body tool.GenBizUpdateReq true "更新业务信息"
// @Produce  application/json
// @Success 200 {object} response.Response{data=tool.GenBizItemResp,msg=string} "{"code": 200, "data": [...]}"
// @Router /tool/codegen/biz [put]
func (g *GenBizCtrl) UpdateGenBizById(c *gin.Context) {
	updateReq := new(tool.GenBizUpdateReq)
	if err := c.ShouldBindJSON(updateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.ToolGroupInst.GenBizServiceInst.UpdateGenBizById(updateReq); err != nil {
		global.MoLogger.Error("更新业务信息失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_BizUpdateFailMsg)
	} else if !ok {
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_CommUpdateFailMsg)
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
	}
}

// DeleteGenBizByIds 删除业务信息
// @Tags 业务信息
// @Summary 删除业务信息
// @Security ApiKeyAuth
// @Param data body []uint64 true "删除业务信息"
// @Produce  application/json
// @Success 200 {object} response.Response{data=tool.GenBizItemResp,msg=string} "{"code": 200, "data": [...]}"
// @Router /tool/codegen/biz/{ids} [delete]
func (g *GenBizCtrl) DeleteGenBizByIds(c *gin.Context) {
	if ids, err := utils.TextToUint64s(c.Param("ids"), ","); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, "参数错误")
	} else {
		if ok, err := services.ServiceGroupInst.ToolGroupInst.GenBizServiceInst.DeleteGenBizByIds(ids); err != nil {
			global.MoLogger.Error("删除业务信息失败", zap.Error(err))
			response.FailErrorWithCodeAsI18n(c, err, consts.CommDeleteFailCode, consts.TKey_BizDeleteFailMsg)
		} else if !ok {
			response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_CommDeleteFailMsg)
		} else {
			response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
		}
	}
}

// GetGenBizPage 分页获取业务信息列表
// @Tags 业务信息
// @Summary 分页获取业务信息列表
// @Security ApiKeyAuth
// @Param data query tool.GenBizPageReq true "分页获取业务信息列表"
// @Produce  application/json
// @Success 200 {object} response.Response{data=tool.GenBizItemResp,msg=string} "{"code": 200, "data": [...]}"
// @Router /tool/codegen/biz/list [get]
func (g *GenBizCtrl) GetGenBizPage(c *gin.Context) {
	genBizPageReq := new(tool.GenBizPageReq)
	if err := c.ShouldBindQuery(genBizPageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	genBizPageReq.ShouldBindParams(c)
	if pageResp, err := services.ServiceGroupInst.ToolGroupInst.GenBizServiceInst.GetGenBizPage(genBizPageReq); err != nil {
		global.MoLogger.Error("获取业务信息列表失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_BizGetFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResp)
	}
}

// GetGenBizById 根据id获取业务信息
// @Tags 业务信息
// @Summary 根据id获取业务信息
// @Security ApiKeyAuth
// @Param data path uint64 true "根据id获取业务信息"
// @Produce  application/json
// @Success 200 {object} response.Response{data=tool.GenBizItemResp,msg=string} "{"code": 200, "data": [...]}"
// @Router /tool/codegen/biz/{id} [get]
func (g *GenBizCtrl) GetGenBizById(c *gin.Context) {
	id := utils.StrToUint64Def(c.Param("id"), 0)
	if genBizItemResp, err := services.ServiceGroupInst.ToolGroupInst.GenBizServiceInst.GetGenBizById(id); err != nil {
		global.MoLogger.Error("获取业务信息失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_BizGetFailMsg)
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, genBizItemResp)
	}
}

// ChangeStatus 修改业务信息状态
// @Tags 业务信息
// @Summary 修改业务信息状态
// @Security ApiKeyAuth
// @Param data body request.ChangeStatusReq true "修改业务信息状态"
// @Produce  application/json
// @Success 200 {object} response.Response{data=tool.GenBizItemResp,msg=string} "{"code": 200, "data": [...]}"
// @Router /tool/codegen/biz/changeStatus [put]
func (g *GenBizCtrl) ChangeStatus(c *gin.Context) {
	changeStatusReq := new(request.ChangeStatusReq)
	if err := c.ShouldBindJSON(changeStatusReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if err := services.ServiceGroupInst.ToolGroupInst.GenBizServiceInst.ChangeStatus(changeStatusReq); err != nil {
		global.MoLogger.Error("修改状态失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_BizStatusUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_BizStatusUpdateSuccessMsg, nil)
}
